package com.mec.ServerCluser.core;

import com.mec.ServerCluser.Interface.IAppServerCilent;
import com.mec.ServerCluser.Interface.IAppServerClientAction;
import com.mec.util.core.ConfigReader;

import java.io.IOException;
import java.net.Socket;

/**
 *      1、作为RMI服务器端，响应客户端远程方法调用服务（我们的框架不需要考虑）。
 *
 *      2、应用服务器需要在建立完成之后，可向注册中心注册发送注册请求
 *             （需要向注册中心说明支持的服务名称、ip（可以通过注册中心服务器直接得到）和port；）。与注册中心建立长连接。
 *
 *      3、向注册中心发送下线请求。
 */
public class AppServerClient implements IAppServerCilent {
    private String serviceName;
    private String appServerIp;
    private int appServerPort;
    private IAppServerClientAction appServerClientAction;
    //以下从配置文件中读
    private static String registryCenterIp;
    private static int registryCenterAppServerClientPort;
    private AppServerClientConversation appServerClientConversation;

    static {
        ConfigReader.init("./net.config.properties");
        registryCenterIp = ConfigReader.getValue("registryCenterIp");
        registryCenterAppServerClientPort =
                Integer.valueOf(ConfigReader.getValue("registryCenterAppServerClientPort"));
    }

    public AppServerClient() {
    }

    public AppServerClient(String serviceName, String appServerIp, int appServerPort) {
        this();
        this.serviceName = serviceName;
        this.appServerIp = appServerIp;
        this.appServerPort = appServerPort;
    }

    @Override
    public void registry() throws Exception {
        if(appServerClientAction == null) {
            appServerClientAction = new AppServerClientActionAdapter();
        }
        Socket socket;
        try {
            socket = new Socket(registryCenterIp, registryCenterAppServerClientPort);
        } catch (IOException e) {
            throw new Exception("连接注册中心失败！");
        }
        appServerClientConversation = new AppServerClientConversation(socket);
        appServerClientConversation.setClient(this);
        //构建标准网络注册信息，进行字符串化，再进行发送
        StringBuffer info = new StringBuffer();
        info.append(serviceName).append("&").append(appServerIp).append("&").append(appServerPort);
        appServerClientConversation.send(new NetMessage()
                .setFrom(appServerIp)
                .setCommand(ENetCommand.APPSERVER_REQUEST_REGISTRY)
                .setTo(registryCenterIp)
                .setAction(null)
                .setParameter(info.toString()));

    }

    @Override
    public void serverLogOut() {
        StringBuffer info = new StringBuffer();
        info.append(serviceName).append("&").append(appServerIp).append("&").append(appServerPort);
        appServerClientConversation.send(new NetMessage()
            .setFrom(appServerIp)
            .setTo(registryCenterIp)
            .setCommand(ENetCommand.APPSERVER_LOGOUT)
            .setParameter(info.toString()));

        appServerClientConversation.stop();
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public String getAppServerIp() {
        return appServerIp;
    }

    public void setAppServerIp(String appServerIp) {
        this.appServerIp = appServerIp;
    }

    public int getAppServerPort() {
        return appServerPort;
    }

    public void setAppServerPort(int appServerPort) {
        this.appServerPort = appServerPort;
    }

    public static String getRegistryCenterIp() {
        return registryCenterIp;
    }

    public IAppServerClientAction getAppServerClientAction() {
        return appServerClientAction;
    }

    public void setAppServerClientAction(IAppServerClientAction appServerClientAction) {
        this.appServerClientAction = appServerClientAction;
    }
}
